/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 28, 2012  1:32:51 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : MmPurchaseOrderServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.MmPurchaseOrder;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class MmPurchaseOrderServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(MmPurchaseOrderServices.class);

	/**
	 * @param em
	 */
	public MmPurchaseOrderServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}

	
	public MmPurchaseOrder findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "MmPurchaseOrder.findById"));				
	}
	
	public List<MmPurchaseOrder> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "MmPurchaseOrder.findByInstanceNo"));				
	}
	
	public List<MmPurchaseOrder> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("MmPurchaseOrder.findAllIsSync", mapWhereValue));				
	}
	
	public List<MmPurchaseOrder> findSapPoDoc(String issync, String instanceNo, String extRefNo, String poStatus) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		mapWhereValue.put("instanceNo", instanceNo);
		mapWhereValue.put("extRefNo", extRefNo);
		mapWhereValue.put("poStatus", poStatus);
		
		return super.ConvertToYoureObject(super.find("MmPurchaseOrder.findSapPoDoc", mapWhereValue));				
	}
	
	public MmPurchaseOrder Updated(MmPurchaseOrder mmVendorMasInb) throws Exception {
		if(mmVendorMasInb != null)
		{
			return (MmPurchaseOrder) super.updated(mmVendorMasInb);
		}else
			return null;
	}
	
	public MmPurchaseOrder InsertedOrUpdated(MmPurchaseOrder vlaue) throws Exception {
		MmPurchaseOrder obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("MM_PURCHASE_ORDERS");					
				obj.setPromisStagingId(id);
//				obj.setCreatedBy(this.getUser());
//				obj.setUpdatedBy(this.getUser());				
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	public List<MmPurchaseOrder> Inserted(List<MmPurchaseOrder> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			if(!CommonService.IsNull(vlauelist))
			{
				for(MmPurchaseOrder obj : vlauelist)
				{
					if(CommonService.IsNull(obj.getPromisStagingId()))
					{
						String id = super.SequenceNext("MM_PURCHASE_ORDERS");
						obj.setPromisStagingId(id);
						//obj.setCreatedBy(this.getUser());
						//obj.setUpdatedBy(this.getUser());				
						obj.setSyncCreatedby(this.getUser());
						obj.setSyncCreatedon(this.getCurrent_date());
						obj.setSyncUpdatedby(this.getUser());
						obj.setSyncUpdatedon(this.getCurrent_date());
						obj.setIsactive("Y");
						obj.setIssync("N");
						common_lst.add(obj);
					}	
				}
				super.inserted(common_lst);
			}			
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	public List<MmPurchaseOrder> Updated(List<MmPurchaseOrder> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmPurchaseOrder obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);					
				}
			}
			
			super.updated(common_lst);
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}


}
